package edu.sdjzu.exam.mapper;

import edu.sdjzu.exam.domain.auth.User;
import edu.sdjzu.exam.dto.UserInfoDTO;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;

import java.util.List;

@Mapper
public interface UserMapper {
    @Select("SELECT * FROM Users WHERE user_id = #{userId}")
    User findById(Long userId);

    @Select("SELECT * FROM Users WHERE username = #{username}")
    User findByUsername(String username);
    
    @Select("SELECT * FROM Users WHERE email = #{email}")
    User findByEmail(String email);

    @Insert("INSERT INTO Users (username, password, email, realname) VALUES (#{username}, #{password}, #{email}, #{realname})")
    @Options(useGeneratedKeys = true, keyProperty = "userId")
    void insert(User user);

    @Update("UPDATE Users SET username = #{username}, password = #{password}, email = #{email}, realname = #{realname} WHERE user_id = #{userId}")
    void update(User user);

    @Delete("DELETE FROM Users WHERE user_id = #{userId}")
    void delete(Long userId);

    @Select("SELECT * FROM Users")
    List<User> findAll();

    /**
     * 根据用户ID查询用户信息和角色信息
     */
    @Select("SELECT u.user_id, u.username, u.email, u.created_at, u.updated_at " +
            "FROM users u WHERE u.user_id = #{userId}")
    @Results(id = "userInfoResultMap", value = {
            @Result(property = "userId", column = "user_id"),
            @Result(property = "username", column = "username"),
            @Result(property = "email", column = "email"),
            @Result(property = "createdAt", column = "created_at"),
            @Result(property = "updatedAt", column = "updated_at"),
            @Result(property = "roles", column = "user_id",
                    many = @Many(select = "selectRolesByUserId", fetchType = FetchType.LAZY))
    })
    UserInfoDTO selectUserInfoWithRolesById(@Param("userId") Long userId);

    /**
     * 根据用户名查询用户信息和角色信息
     */
    @Select("SELECT u.user_id, u.username, u.email, u.created_at, u.updated_at " +
            "FROM users u WHERE u.username = #{username}")
    @ResultMap("userInfoResultMap")
    UserInfoDTO selectUserInfoWithRolesByUsername(@Param("username") String username);

    /**
     * 根据用户ID查询角色信息
     */
    @Select("SELECT r.role_id, r.role_name, r.description " +
            "FROM roles r " +
            "INNER JOIN user_roles ur ON r.role_id = ur.role_id " +
            "WHERE ur.user_id = #{userId}")
    @Results({
            @Result(property = "roleId", column = "role_id"),
            @Result(property = "roleName", column = "role_name"),
            @Result(property = "description", column = "description")
    })
    List<UserInfoDTO.RoleInfo> selectRolesByUserId(@Param("userId") Long userId);

    /**
     * 检查用户是否存在
     */
    @Select("SELECT COUNT(*) FROM users WHERE user_id = #{userId}")
    int checkUserExists(@Param("userId") Long userId);

    /**
     * 检查用户名是否存在
     */
    @Select("SELECT COUNT(*) FROM users WHERE username = #{username}")
    int checkUsernameExists(@Param("username") String username);
}
